<template>
  <div class="table-container">
    <vab-query-form>
      <!--      <vab-query-form-right-panel>-->
      <el-form
        ref="form"
        :model="userForm"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item label="姓名">
          <el-input v-model="userForm.realName" clearable placeholder="姓名" />
        </el-form-item>

        <el-form-item label="性别">
          <el-select v-model="userForm.sex" clearable placeholder="请选择">
            <el-option
              v-for="item in sexList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="校友身份">
          <el-select v-model="userForm.identity" clearable placeholder="请选择">
            <el-option
              v-for="item in identityList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="行业领域">
          <el-select
            v-model="userForm.occupationalId"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in occupationalList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="入学年份">
          <el-date-picker
            v-model="userForm.enterTime"
            type="date"
            placeholder="选择日期"
            style="width: 200px"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="专业和班级">
          <el-input
            v-model="userForm.majorClass"
            clearable
            placeholder="专业和班级"
          />
        </el-form-item>

        <el-form-item label="openID">
          <el-input v-model="userForm.openId" clearable placeholder="openID" />
        </el-form-item>

        <el-button
          icon="el-icon-search"
          type="primary"
          native-type="submit"
          @click="handleQuery"
        >
          查询
        </el-button>
      </el-form>
      <!--      </vab-query-form-right-panel>-->
      <!-- <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
        添加
      </el-button> -->
      <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
        批量删除
      </el-button>
    </vab-query-form>

    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
      @selection-change="setSelectRows"
      @sort-change="tableSortChange"
    >
      <el-table-column
        show-overflow-tooltip
        type="selection"
        width="55"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="序号"
        width="95"
        header-align="center"
        align="center"
      >
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="realName"
        label="真实姓名"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="头像"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-image
            v-if="imgShow"
            :preview-src-list="imageList"
            :src="row.headUrl"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="sex"
        label="性别"
        header-align="center"
        align="center"
      >
        <template slot-scope="scope">
          {{ scope.row.sex === '0' ? '女' : '男' }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="identity"
        label="校友身份"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.identity === '就读' ? 'primary' : 'success'"
            disable-transitions
          >
            {{ scope.row.identity === '就读' ? '就读' : '教职工' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="birthday"
        label="出生年月"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="majorClass"
        label="专业和班级"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="enterTime"
        label="入学年份"
        width="200"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="occupationalName"
        label="行业领域"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="openId"
        label="openID"
        header-align="center"
        align="center"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="操作"
        width="180px"
        header-align="center"
        align="center"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="userForm.page"
      :layout="layout"
      :page-size="userForm.size"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </div>
</template>

<script>
  import {
    userSelectBy,
    userDeleteByIds,
    occupationalSelectView,
  } from '@/api/management/management'
  export default {
    name: 'UserManagement',
    components: {},
    filters: {},
    data() {
      return {
        imgShow: true,
        list: [],
        imageList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        userForm: {
          page: 1,
          size: 20,
          realName: '',
          sex: '',
          identity: '',
          birthday: '',
          liveCity: '',
          nativePlace: '',
          majorClass: '',
          enterTime: '',
          graduateTime: '',
          occupationalId: '',
          occupationalName: '',
        },
        occupationalList: [],
        identityList: [
          {
            value: '就读',
            label: '就读',
          },
          {
            value: '教职工',
            label: '教职工',
          },
        ],
        sexList: [
          {
            value: 1,
            label: '男',
          },
          {
            value: 0,
            label: '女',
          },
        ],
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
      //获取 行业领域
      this.getOccupationalList()
    },
    beforeDestroy() {},
    mounted() {},
    methods: {
      tableSortChange() {
        const imageList = []
        this.$refs.tableSort.tableData.forEach((item, index) => {
          imageList.push(item.img)
        })
        this.imageList = imageList
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleAdd() {
        // this.$refs['edit'].showEdit()
        this.$router.push({
          path: '/editUser',
          query: '',
        })
      },
      handleEdit(row) {
        // this.$refs['edit'].showEdit(row)
        this.$router.push({
          path: '/editUser',
          query: {
            row: row,
          },
        })
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            await userDeleteByIds([row.openId]).then((res) => {
              if (res.code == 200) {
                this.$baseMessage(res.data, 'success')
                this.fetchData()
              } else {
                this.$baseMessage('删除失败', 'error')
              }
            })
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.openId)
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              await userDeleteByIds(ids).then((res) => {
                if (res.code == 200) {
                  this.$baseMessage(res.data, 'success')
                  this.fetchData()
                } else {
                  this.$baseMessage('删除失败', 'error')
                }
              })
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.userForm.size = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.userForm.page = val
        this.fetchData()
      },
      handleQuery() {
        this.userForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        debugger
        this.listLoading = true
        await userSelectBy(this.userForm).then((res) => {
          if (res.code === 200) {
            this.list = res.data
            const imageList = []
            res.data.forEach((item, index) => {
              imageList.push(item.headUrl)
            })
            this.imageList = imageList
            //总数，决定页数
            this.total = 50
          } else {
            this.$baseMessage('数据获取失败', 'error')
          }
        })
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      //获取 行业领域
      async getOccupationalList() {
        await occupationalSelectView().then((res) => {
          if (res.code == 200) {
            this.occupationalList = res.data
          } else {
            this.$baseMessage('行业领域获取失败', 'error')
          }
        })
      },
    },
  }
</script>
